package com.ficus.road.maintain.controller.smart;

import com.ficus.road.maintain.core.model.smartnew.PrimaryRoadSectionDTO;
import com.ficus.road.maintain.dao.smartnew.primaryScreening.PrimaryRoadSectionDao;
import com.ficus.road.maintain.service.smartnew.primaryScreening.PrimaryRoadSectionService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class SmartV3Controller {
    private final PrimaryRoadSectionDao primaryRoadSectionDao;
    private final PrimaryRoadSectionService primaryRoadSectionService;
    /**
     * 项目初筛（公路）
     *
     */
    @RequestMapping(value = "/smartDiscovery/highway/prescreening",
            produces = {"application/json"},
            method = RequestMethod.GET)
    @PreAuthorize("@rm.check('SMART')")
    public ResponseEntity<List<PrimaryRoadSectionDTO>> listHighwayInitialScreening() {
        Query query = new Query();
        query.fields().exclude("gdzb").exclude("hit_ids").exclude("coordinates");
        query.with(Sort.by(Sort.Direction.ASC, "score"));
        List<PrimaryRoadSectionDTO> byQuery = primaryRoadSectionDao.findByQuery(query);
        return new ResponseEntity<>(byQuery, HttpStatus.OK);
    }

    @RequestMapping(value = "/smartDiscovery/highway/prescreeningwithDirection",
            produces = {"application/json"},
            method = RequestMethod.GET)
    public ResponseEntity<List<PrimaryRoadSectionDTO>> listHighwayInitialScreeningWithDirection() {
        Query query = new Query();
        query.fields().exclude("gdzb").exclude("hit_ids").exclude("coordinates");
        query.with(Sort.by(Sort.Direction.ASC, "score"));
        List<PrimaryRoadSectionDTO> byQuery = primaryRoadSectionDao.findByQuery(query);
        return new ResponseEntity<>(byQuery, HttpStatus.OK);
    }

    @RequestMapping(value = "/dataFlush/refreshScore",
            method = RequestMethod.GET)
    public ResponseEntity<String> refreshGps() {
        primaryRoadSectionService.calculate();
        return new ResponseEntity<>("success", HttpStatus.OK);
    }

}
